package cn.tedu.file;

import java.io.*;

/*本类用于测试字符输出流*/
public class TestOut2 {
    public static void main(String[] args) {
//        method1();//用于测试普通字符输出流
        method2();//用于测试高效字符输出流
    }

    private static void method2() {
        //1.定义局部变量
        BufferedWriter out = null;
        //2.完成异常处理结构
        try{
            //3.1会覆盖输出的创建高效输出流(字符流输出对象)对象
            //out = new BufferedWriter(new FileWriter(new File("D:\\ready\\1.txt")));
            //out = new BufferedWriter(new FileWriter("D:\\ready\\1.txt"));
            //3.2会追加输出的创建高效输出流(字符流输出对象)对象
            //out = new BufferedWriter(new FileWriter(new File("D:\\ready\\1.txt"),true));
            out = new BufferedWriter(  //BufferedWriter extends Writer
                    //FileWriter extends OutputStreamWriter extends Writer,,,Constructor(File or String)
                    new FileWriter("D:\\ready\\1.txt",true));//设置实参为true
            //4.使用流对象
            out.write(98);
            out.write(98);
            out.write(98);
            out.write(98);
            out.write(98);
        }catch (IOException e){
            e.printStackTrace(); //1)率先设计的程序结构和框架(否则直接是runtimeException),3)如果捕获到异常就打印错误信息(捕获异常就打印错误信息),2)提供给程序员看,,,
        } /*catch (IOException e) {
            throw new RuntimeException(e);
        }*/ finally {
            //5.关流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static void method1() {
        //1.定义局部变量
        /*FileWriter*/
        Writer out = null;
        //2.完成异常捕获处理结构
        try{
            //3.1创建覆盖输出的字符流对象
            //out = new FileWriter(new File("D:\\ready\\1.txt"));
            out = new FileWriter("D:\\ready\\1.txt");
            //3.2创建追加输出的字符流对象
            //out = new FileWriter(new File("D:\\ready\\1.txt"),true);
//            out = new FileWriter("D:\\ready\\1.txt",true); //覆盖或追加输出效果
            //4.使用流对象输出数据
            out.write(99);
            out.write(99);
            out.write(99);
            out.write(99);
        }catch (Exception e){
            e.printStackTrace();
        } /*catch (IOException e) {
            throw new RuntimeException(e);
        }*/ finally {
            //5.关流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
